﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Ewts.Code
{
    public class MyCheckBox
    {
        /// <summary>
        /// 获取符合条件的收货员信息
        /// </summary>
        /// <param name="where">区域</param>
        /// <returns></returns>
        public static List<System.Web.UI.Control> GetCheckBoxListByVehiclor(string city)
        {
            List<System.Web.UI.Control> controls = new List<System.Web.UI.Control>();
            DataTable dt = new DataLib.BLL.Vehiclor().GetList("city='" + city+"'").Tables[0];
            if (dt == null)
            {
                return null;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                System.Web.UI.WebControls.CheckBox chk = new System.Web.UI.WebControls.CheckBox();
                chk.Text = dt.Rows[i]["name"].ToString();
                chk.ID = "chk_" + dt.Rows[i]["id"].ToString();
                
                controls.Add(chk);

            }
            return controls;
        }


        /// <summary>
        /// 获取符合条件的车牌号信息
        /// </summary>
        /// <param name="where">区域</param>
        /// <returns></returns>
        public static List<System.Web.UI.Control> GetCheckBoxListByPlateNumber(string city)
        {
            List<System.Web.UI.Control> controls = new List<System.Web.UI.Control>();
            DataTable dt = new DataLib.BLL.PlateNumber().GetList("city='" + city +"'").Tables[0];
            if (dt == null)
            {
                return null;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                System.Web.UI.WebControls.CheckBox chk = new System.Web.UI.WebControls.CheckBox();
                int tranid = int.Parse(dt.Rows[i]["transportIid"].ToString());
                string text = "";
                text += new DataLib.BLL.TransportCompany().GetModel(tranid).ShortName + "-";
                text += dt.Rows[i]["maxton"].ToString() + "-";
                text += dt.Rows[i]["plateno"].ToString();
                chk.Text =text;
                chk.ID = "chk_" + dt.Rows[i]["id"].ToString();

                controls.Add(chk);

            }
            return controls;
        }


        /// <summary>
        /// 获取符合条件的车牌号信息
        /// </summary>
        /// <param name="where">区域</param>
        /// <returns></returns>
        public static DataTable GetDataSourceByCity(string where)
        {
           
             DataTable dt = new DataLib.BLL.PlateNumber().GetList(where).Tables[0];
            if (dt == null)
            {
                return null;
            }
            DataLib.BLL.TransportCompany blltc = new DataLib.BLL.TransportCompany();
            dt.Columns.Add("ShortName");
            int tranid = 0;
            string shortname = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tranid = int.Parse(dt.Rows[i]["transportIid"].ToString());
                shortname =  blltc.GetModel(tranid).ShortName;
                shortname += "-";
                shortname += dt.Rows[i]["vehicletype"].ToString();
                shortname += "-";
                shortname += dt.Rows[i]["maxton"].ToString();
                shortname += "-";
                shortname += dt.Rows[i]["plateno"].ToString();
                
                dt.Rows[i]["ShortName"] = shortname;

            }
            return dt;
        }

        /// <summary>
        /// 根据控件集获取checkbox的id值的集合
        /// </summary>
        /// <param name="chkList"></param>
        /// <param name="flag">0代表收货员，1代表车牌号</param>
        /// <returns>ID值集合</returns>
        public static string GetCheckIDColl(System.Web.UI.WebControls.CheckBoxList chkList, int flag)
        {
            string idcoll = "";
            int id = 0;
            foreach (System.Web.UI.WebControls.ListItem chk in chkList.Items)
            {
                 if (chk.Selected == true)
                {
                    id = int.Parse(chk.Value);
                     //0代表收货员
                    if (flag == 0)
                    {
                        DataLib.Model.Vehiclor vmodel = new DataLib.BLL.Vehiclor().GetModel(id);
                        vmodel.currentstatus += 1;
                        new DataLib.BLL.Vehiclor().Update(vmodel);
                    }
                    else
                    {
                        DataLib.Model.PlateNumber pmodel = new DataLib.BLL.PlateNumber().GetModel(id);
                        pmodel.currentstatus += 1;
                        new DataLib.BLL.PlateNumber().Update(pmodel);
                    }
                    idcoll += chk.Value.Trim();
                    idcoll += ",";
                }
               
            }
            if (idcoll.IndexOf(',') > 1)
            {
                idcoll.Remove(idcoll.Length - 1);
            }
            return idcoll;
        }

        /// <summary>
        /// 根据控件集获取checkbox的id值的集合
        /// </summary>
        /// <param name="chkList"></param>
        /// <param name="flag">0代表收货员，1代表车牌号</param>
        /// <param name="editstatus">是否是修改状态</param>
        /// <returns>ID值集合</returns>
        public static string GetCheckIDColl(System.Web.UI.WebControls.CheckBoxList chkList, int flag, bool editstatus)
        {
            string idcoll = "";
            int id = 0;
            foreach (System.Web.UI.WebControls.ListItem chk in chkList.Items)
            {
                if (chk.Selected == true)
                {
                    id = int.Parse(chk.Value);
                    //0代表收货员
                    if (flag == 0)
                    {
                        DataLib.Model.Vehiclor vmodel = new DataLib.BLL.Vehiclor().GetModel(id);
                        if(editstatus==true)
                            vmodel.currentstatus += 1;
                        new DataLib.BLL.Vehiclor().Update(vmodel);
                    }
                    else
                    {
                        DataLib.Model.PlateNumber pmodel = new DataLib.BLL.PlateNumber().GetModel(id);
                        if(editstatus==true)
                            pmodel.currentstatus += 1;
                        new DataLib.BLL.PlateNumber().Update(pmodel);
                    }
                    idcoll += chk.Value.Trim();
                    idcoll += ",";
                }

            }
            if (idcoll.IndexOf(',') > 1)
            {
                idcoll.Remove(idcoll.Length - 1);
            }
            return idcoll;
        }
    }
}